<template>
  <div class="ratings" ref="ratings">
    <div class="ratings-content">
      <div class="overview">
        <div class="overview-left">
          <div class="score">{{seller.score}}</div>
          <div class="text">综合评分</div>
          <div class="rankRate">高于周边商家{{seller.rankRate}}%</div>
        </div>
        <div class="overview-right">
          <div class="score-wrapper">
            <span class="text">服务态度</span>
            <star :size="36" :score="seller.serviceScore"></star>
            <span class="score">{{seller.serviceScore}}</span>
          </div>
          <div class="score-wrapper">
            <span class="text">商品评分</span>
            <star :size="36" :score="seller.foodScore"></star>
            <span class="score">{{seller.foodScore}}</span>
          </div>
          <div class="delivery-time">
            <span class="text">送达时间</span>
            <span class="time">{{seller.deliveryTime}}分钟</span>
          </div>
        </div>
      </div>
      <split></split>
      <ratingselect :select-type="selectType" :only-content="onlyContent" :ratings="ratings"></ratingselect>
      <ul class="rating-list">
        <li v-show="needShow(rating.rateType, rating.text)" v-for="(rating, key) in ratings" :key="key" class="rating-item border-1px">
          <div class="rating-left">
            <img :src="rating.avatar" width="28" height="28" class="avatar">
          </div>
          <div class="rating-right">
            <div class="user-info">
              <span class="user">{{rating.username}}</span>
              <span class="time">{{rating.rateTime | formatDate}}</span>
            </div>
            <div class="score-info">
              <star :size="24" :score="rating.score"></star>
              <span v-show="rating.deliveryTime" class="delivery-time">{{rating.deliveryTime}}分钟送达</span>
            </div>
            <p class="text">{{rating.text}}</p>
            <div class="recommends" v-show="rating.recommend && rating.recommend.length">
              <i class="icon-thumb_up"></i>
              <div class="recommend-list">
                <span v-for="(recommend, index) in rating.recommend" :key="index" class="recommend-item">{{recommend}}</span>
              </div>
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll';
import star from 'components/star/star';
import split from 'components/split/split';
import ratingselect from 'components/ratingselect/ratingselect';
import { formatDate } from 'common/js/date';

const axios = require('axios');
const ERR_OK = 0;
const ALL = 2;

export default {
  props: {
    seller: {
      type: Object,
      default () {
        return {};
      }
    }
  },
  data () {
    return {
      ratings: [],
      selectType: ALL,
      onlyContent: false
    };
  },
  created () {
    const _this = this;
    axios.get('/api/ratings')
      .then(function (response) {
        if (response.data.status === ERR_OK) {
          _this.ratings = response.data.data;
          _this.$nextTick(() => {
            if (!_this.scroll) {
              _this.scroll = new BScroll(_this.$refs.ratings, {
                click: true
              });
            } else {
              _this.scroll.refresh();
            }
          });
        }
      });
  },
  methods: {
    needShow (type, text) {
      this.$root.eventHub.$on('ratingtypeselect', (type) => {
        this.selectType = type;
        // this.$nextTick(() => {
        //   this.scroll.refresh();
        // });
      });
      this.$root.eventHub.$on('contentshow', (content) => {
        this.onlyContent = content;
        // this.$nextTick(() => {
        //   this.scroll.refresh();
        // });
      });
      if (this.onlyContent && !text) {
        return false;
      }
      if (this.selectType === ALL) {
        return true;
      } else {
        return this.selectType === type;
      }
    }
  },
  filters: {
    formatDate (time) {
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm');
    }
  },
  components: {
    star,
    split,
    ratingselect
  }
};
</script>

<style lang="stylus" scoped="scoped">
@import 'common/stylus/mixin';
  .ratings
    position: absolute
    top: 179px
    bottom: 0
    left: 0
    width: 100%
    overflow: hidden
    .overview
      padding: 18px 0
      display: flex
      .overview-left
        flex: 0 0 137px
        width: 137px
        text-align: center
        border-right: 1px solid rgba(7, 17, 27, .1)
        @media only screen and (max-width: 320px)
          flex: 0 0 120px
          width: 120px
        .score
          margin-bottom: 6px
          font-size: 24px
          line-height: 28px
          color: rgb(255, 153, 0)
        .text
          margin-bottom: 8px
          font-size: 12px
          line-height: 12px
          color: rgb(7, 17, 27)
        .rankRate
          margin-bottom: 6px
          font-size: 10px
          line-height: 10px
          color: rgb(147, 153, 159)
      .overview-right
        flex: 1
        padding-left: 24px
        @media only screen and (max-width: 320px)
          padding-left: 6px
        .score-wrapper
          margin-bottom: 8px
          font-size: 0
          .text
            display: inline-block
            font-size: 12px
            line-height:18px
            vertical-align: top
            color: rgb(7, 17, 27)
          .star
            display: inline-block
            vertical-align: top
            margin: 0 12px
            // &.star-36 .star-item
            //   margin-right: 4px
          .score
            display: inline-block
            font-size: 12px
            line-height:18px
            vertical-align: top
            color: rgb(255, 153, 0)
        .delivery-time
          font-size: 0
          .text
            display: inline-block
            line-height: 18px
            margin-right: 12px
            font-size: 12px
            color: rgb(7, 17, 27)
          .time
            display: inline-block
            line-height: 18px
            font-size: 12px
            color: rgb(147, 153, 159)
    .rating-list
      padding: 0 18px
      .rating-item
        display: flex
        padding: 18px 0
        border-bottom-1px(rgba(7, 17, 27, .1))
        .rating-left
          flex: 0 0 28px
          width: 28px
          margin-right: 12px
          .avatar
            border-radius: 50%
        .rating-right
          flex: 1
          .user-info
            display: flex
            margin-bottom: 4px
            font-size: 10px
            line-height: 12px
            .user
              flex: 1
              color: rgb(7, 17, 27)
            .time
              flex: 1
              text-align: right
              color: rgb(147, 153, 159)
          .score-info
            height: 12px
            margin-bottom: 6px
            .star
              display: inline-block
              margin-right: 6px
              vertical-align: top
            .delivery-time
              display: inline-block
              vertical-align: top
              font-size: 10px
              line-height: 12px
              color: rgb(147, 153, 159)
          .text
            margin-bottom: 8px
            font-size: 12px
            line-height: 18px
            color: rgb(7, 17, 27)
          .recommends
            display: flex
            .icon-thumb_down, .icon-thumb_up
              flex: 0 0 12px
              width: 12px
              margin-right: 8px
              font-size: 12px
              line-height: 16px
              &.icon-thumb_up
                color: rgb(0, 160, 220)
              &.icon-thumb_down
                color: rgb(183, 187, 191)
            .recommend-list
              flex: 1
              .recommend-item
                display: inline-block
                margin: 0 8px 8px 0
                padding: 0 6px
                font-size: 9px
                line-height: 16px
                color: rgb(147, 153, 159)
                border-radius: 1px
                border: 1px solid rgba(7, 17, 27, .1)
</style>
